package com.team2.hospital.controller;

import com.alibaba.fastjson.JSONObject;
import com.team2.hospital.entity.Doctor;
import com.team2.hospital.entity.Orders;
import com.team2.hospital.service.OrderService;
import com.team2.hospital.service.DoctorService;
import com.team2.hospital.utils.ResponseData;
import com.team2.hospital.utils.TodayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private DoctorService doctorService;

    /**
     * 判断是否能进行预约缴费
     *
     * @param oId 订单ID
     * @return 返回是否可以进行缴费
     */
    @GetMapping("canPay")
    public ResponseData canPay(@RequestParam("oId") int oId) {
        System.out.println("[canPay] 请求开始，订单ID：" + oId);
        if (this.orderService.canPay(oId)) {
            return ResponseData.success("可以缴费");
        }
        return ResponseData.fail("当前不在缴费时间窗口内");
    }

    /**
     * 查询医生的预约队列
     *
     * @param dId 医生ID
     * @return 返回医生的队列信息
     */
    @GetMapping("queue")
    public ResponseData queue(@RequestParam("dId") int dId) {
        System.out.println("[queue] 请求开始，医生ID：" + dId);
        Doctor doctor = doctorService.findDoctor(dId);
        if (doctor == null) {
            return ResponseData.fail("预约医生不存在");
        }
        List<Orders> queue = orderService.findQueueByDID(dId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dName", doctor.getdName());
        jsonObject.put("dSection", doctor.getdSection());
        jsonObject.put("queueData", queue);
        return ResponseData.success("查询队列成功", jsonObject);
    }


    /**
     * 根据订单ID更新挂号信息
     *
     * @param orders 订单信息
     * @return 返回更新结果
     */
    @PostMapping("updateOrder")
    @ResponseBody
    public ResponseData updateOrder(@RequestBody Orders orders) {
        System.out.println("[updateOrder] 请求开始，订单ID：" + orders.getOId());
        if (this.orderService.updateOrder(orders)) {
            return ResponseData.success("更新挂号信息成功");
        }
        return ResponseData.fail("更新挂号信息失败！");
    }


    /**
     * 根据订单ID设置缴费状态
     *
     * @param oId 订单ID
     * @return 返回设置缴费状态的结果
     */
    @RequestMapping("updatePrice")
    public ResponseData updatePrice(int oId) {
        System.out.println("[updatePrice] 请求开始，订单ID：" + oId);
        if (this.orderService.updatePrice(oId)) {
            return ResponseData.success("根据ID设置缴费状态成功");
        }
        return ResponseData.fail("根据ID设置缴费状态失败");
    }

    /**
     * 查找医生已完成的挂号单
     *
     * @param pageNumber 页码
     * @param size       每页大小
     * @param query      查询条件
     * @param dId        医生ID
     * @return 返回医生已完成的挂号单信息
     */
    @RequestMapping("findOrderFinish")
    public ResponseData findOrderFinish(int pageNumber, int size, String query, int dId) {
        System.out.println("[findOrderFinish] 请求开始，医生ID：" + dId + "，页码：" + pageNumber + "，每页大小：" + size);
        return ResponseData.success("查找医生已完成的挂号单完成！", this.orderService.findOrderFinish(pageNumber, size, query, dId));
    }

    /**
     * 根据医生ID查询挂号信息
     *
     * @param pageNumber 页码
     * @param size       每页大小
     * @param query      查询条件
     * @param dId        医生ID
     * @return 返回挂号信息
     */
    @RequestMapping("findOrderByDid")
    public ResponseData findOrderByDid(int pageNumber, int size, String query, int dId) {
        System.out.println("[findOrderByDid] 请求开始，医生ID：" + dId + "，页码：" + pageNumber + "，每页大小：" + size);
        return ResponseData.success("返回挂号信息成功", this.orderService.findOrderByDid(pageNumber, size, query, dId));
    }

    /**
     * 统计今天挂号人数
     *
     * @return 返回今天挂号人数
     */
    @RequestMapping("orderPeople")
    public ResponseData orderPeople() {
        String oStart = TodayUtil.getTodayYmd();
        System.out.println("[orderPeople] 请求开始，日期：" + oStart);
        return ResponseData.success("统计今天挂号人数成功", Optional.of(this.orderService.orderPeople(oStart)));
    }

    /**
     * 统计今天某个医生挂号人数
     *
     * @param dId 医生ID
     * @return 返回今天某个医生挂号人数
     */
    @RequestMapping("orderPeopleByDid")
    public ResponseData orderPeopleByDid(int dId) {
        String oStart = TodayUtil.getTodayYmd();
        System.out.println("[orderPeopleByDid] 请求开始，医生ID：" + dId + "，日期：" + oStart);
        return ResponseData.success("统计今天挂号人数成功", Optional.of(this.orderService.orderPeopleByDid(oStart, dId)));
    }

    /**
     * 获取过去20天的挂号人数
     *
     * @return 返回过去20天的挂号人数
     */
    @RequestMapping("orderSeven")
    public ResponseData orderSeven() {
        ArrayList<Integer> list = new ArrayList<>();
        System.out.println("[orderSeven] 请求开始，过去20天的挂号统计");
        for (int i = 20; i > 0; i--) {
            String oStart = TodayUtil.getPastDate(i);
            int people = this.orderService.orderPeople(oStart);
            list.add(people);
        }
        return ResponseData.success("获取过去20天的挂号人数成功", list);
    }

    /**
     * 统计挂号的男女比例
     *
     * @return 返回挂号的男女比例
     */
    @RequestMapping("orderGender")
    public ResponseData orderGender() {
        System.out.println("[orderGender] 请求开始，统计挂号性别人数");
        return ResponseData.success("统计挂号男女人数", this.orderService.orderGender());
    }

    /**
     * 增加诊断及医生意见
     *
     * @param order 订单信息
     * @return 返回增加诊断及医生意见的结果
     */
    @PostMapping("updateOrderByAdd")
    @ResponseBody
    public ResponseData updateOrderByAdd(@RequestBody Orders order) {
        System.out.println("[updateOrderByAdd] 请求开始，订单ID：" + order.getOId());
        if (this.orderService.updateOrderByAdd(order)) {
            return ResponseData.success("增加诊断及医生意见成功");
        }
        return ResponseData.fail("增加诊断及医生意见失败");
    }

    /**
     * 判断诊断后是否需要缴费
     *
     * @param oId 订单ID
     * @return 返回是否需要缴费
     */
    @RequestMapping("findTotalPrice")
    public ResponseData findTotalPrice(int oId) {
        System.out.println("[findTotalPrice] 请求开始，订单ID：" + oId);
        if (this.orderService.findTotalPrice(oId)) {
            return ResponseData.success("未缴费");
        }
        return ResponseData.fail("无需缴费");
    }

    /**
     * 请求挂号时间段
     *
     * @param arId 排班ID
     * @return 返回挂号时间段信息
     */
    @RequestMapping("findOrderTime")
    public ResponseData findOrderTime(String arId) {
        System.out.println("[findOrderTime] 请求开始，排班ID：" + arId);
        return ResponseData.success("请求挂号时间段成功", this.orderService.findOrderTime(arId));
    }

    /**
     * 统计过去20天挂号科室人数
     *
     * @return 返回过去20天挂号科室人数
     */
    @RequestMapping("orderSection")
    public ResponseData orderSection() {
        System.out.println("[orderSection] 请求开始，统计过去20天挂号科室人数");
        return ResponseData.success("统计过去20天挂号科室人数成功", this.orderService.orderSection());
    }

    /**
     * 改变订单状态为完成
     *
     * @param oId 订单ID
     * @return 返回操作结果
     */
    @RequestMapping("changeFinish")
    public ResponseData changeFinish(@RequestParam(value = "oId") int oId) {
        System.out.println("[changeFinish] 请求开始，订单ID：" + oId);
        Boolean bo = this.orderService.changeFinish(oId);
        if (bo) {
            return ResponseData.success("审核成功");
        }
        return ResponseData.fail("审核失败");
    }

}
